cmake_minimum_required(VERSION 3.30)
project(App)

set(CMAKE_CXX_STANDARD 20)

# 创建可执行文件，并链接静态库和动态库，包括第三方库
add_executable(App app.cpp)

# 链接静态库和动态库，包括第三方库
target_link_libraries(App PRIVATE MatrixLib MatrixDll)

# 包含头文件目录
target_include_directories(App PRIVATE ../matrix_lib ../matrix_dll)

# 查找第三方库和头文件
if (WIN32)
    set(MATRIX_SUB_LIBRARY ${PROJECT_SOURCE_DIR}/../3part/matrix_sub/libmatrix_sub.dll)
    if (EXISTS ${MATRIX_SUB_LIBRARY})
        target_link_libraries(App PRIVATE ${MATRIX_SUB_LIBRARY})
        include_directories(${PROJECT_SOURCE_DIR}/../3part/matrix_sub)
    else ()
        message(WARNING "matrix_sub.dll not found at ${MATRIX_SUB_LIBRARY}")
        set(MATRIX_SUB_LIBRARY "")
    endif ()

    set(MATRIX_SUB_DOUBLE_LIBRARY ${PROJECT_SOURCE_DIR}/../3part/matrix_sub_double/libmatrix_sub_double.a)
    if (EXISTS ${MATRIX_SUB_DOUBLE_LIBRARY})
        target_link_libraries(App PRIVATE ${MATRIX_SUB_DOUBLE_LIBRARY})
        include_directories(${PROJECT_SOURCE_DIR}/../3part/matrix_sub_double)
    else ()
        message(WARNING "libmatrix_sub_double.a not found at ${MATRIX_SUB_DOUBLE_LIBRARY}")
        set(MATRIX_SUB_DOUBLE_LIBRARY "")
    endif ()
endif ()

# 定义复制 DLL 文件的函数
function(copy_dll target src_path)
    add_custom_command(TARGET ${target} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${src_path}"
            "$<TARGET_FILE_DIR:${target}>"
    )
endfunction()

# 使用统一的方法复制 DLL 文件
copy_dll(App "$<TARGET_FILE:MatrixDll>")
copy_dll(App "${PROJECT_SOURCE_DIR}/../3part/matrix_sub/libmatrix_sub.dll")

## 复制 DLL 文件到可执行文件目录
#add_custom_command(TARGET App POST_BUILD
#        COMMAND ${CMAKE_COMMAND} -E copy_if_different
#        "$<TARGET_FILE:MatrixDll>"
#        "$<TARGET_FILE_DIR:App>"
#)
## 复制 三方dll 文件到可执行文件目录
#add_custom_command(TARGET App POST_BUILD
#        COMMAND ${CMAKE_COMMAND} -E copy_if_different
#        "${PROJECT_SOURCE_DIR}/../3part/matrix_sub/libmatrix_sub.dll"
#        "$<TARGET_FILE_DIR:App>"
#)
